<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>注册</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f0f4f8;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
    .card {
      background-color: white;
      border-radius: 8px;
      box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
      padding: 2rem;
      width: 100%;
      max-width: 400px;
    }
    h1 {
      color: #333;
      text-align: center;
      margin-bottom: 0.5rem;
    }
    p {
      color: #666;
      text-align: center;
      margin-bottom: 1.5rem;
    }
    form {
      display: flex;
      flex-direction: column;
    }
    input {
      padding: 0.75rem;
      margin-bottom: 1rem;
      border: 1px solid #ddd;
      border-radius: 4px;
      font-size: 1rem;
    }
    button {
      background-color: #4a90e2;
      color: white;
      padding: 0.75rem;
      border: none;
      border-radius: 4px;
      font-size: 1rem;
      cursor: pointer;
      transition: background-color 0.3s;
    }
    button:hover {
      background-color: #3a7bd5;
    }
    .login-link {
      text-align: center;
      margin-top: 1rem;
      font-size: 0.9rem;
    }
    a {
      color: #4a90e2;
      text-decoration: none;
    }
    a:hover {
      text-decoration: underline;
    }
    .error {
      color: red;
      text-align: center;
      margin-top: 1rem;
      display: none; /* 默认隐藏错误信息 */
    }
  </style>
</head>
<body>
<div class="card">
  <h1>注册</h1>
  <p>创建您的账号</p>
  <form id="registerForm" method="post">
    <input type="text" name="username" placeholder="用户名" required>
    <input type="password" name="password" placeholder="密码" required>
    <input type="password" name="confirm-password" placeholder="确认密码" required>
    <button type="submit">注册</button>
  </form>
  <div class="login-link">
    已有账号? <a href="index.html">登录</a>
  </div>
  <div class="error" id="errorMessage"></div>
</div>
<script>
  document.getElementById('registerForm').onsubmit = async function(event) {
    event.preventDefault(); // 阻止默认表单提交
    const formData = new URLSearchParams(new FormData(this)).toString(); // URL 编码表单数据
    const response = await fetch('/user/register', {
      method: 'POST',
      body: formData,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded', // 设置请求头
        'Accept': 'application/json'
      }
    });

    if (response.ok) {
      window.location.href = '/index.html'; // 注册成功后重定向到登录页面
    } else {
      const errorMessage = await response.json();
      document.getElementById('errorMessage').innerText = errorMessage.error;
      document.getElementById('errorMessage').style.display = 'block'; // 显示错误信息
    }
  };
</script>
</body>
</html>
